"use strict"

var koa = require('koa')
var path = require('path')
var wechat = require('./wechat/g')
var reply = require('./wx/reply')
var mongoose = require('mongoose')
var fs = require('fs')

var dbUrl = "mongodb://localhost/E/MongoDB"
mongoose.connect(dbUrl)

// models loading
var models_path = __dirname + '/app/models'
var walk = function(path) {
  fs
    .readdirSync(path)
    .forEach(function(file) {
      var newPath = path + '/' + file
      var stat = fs.statSync(newPath)

      if (stat.isFile()) {
        if (/(.*)\.(js|coffee)/.test(file)) {
          require(newPath)
        }
      }
      else if (stat.isDirectory()) {
        walk(newPath)
      }
    })
}
walk(models_path)

var menu =require('./wx/menu')
var wx = require('./wx/index')
var wechatApi = wx.getWechat()


wechatApi.deleteMenu().then(function(){
	return wechatApi.createMenu(menu)
})
.then(function(msg){
	console.log(msg)
})

var app = new koa()
var Router = require('koa-router')
var router = new Router()
var game = require('./app/controllers/game')

router.get('/movie',game.movie)

app
	.use(router.routes())
	.use(router.allowedMethods())

app.use(wechat(wx.wechatOptions.wechat,reply.reply))

app.listen(80)
console.log('listening:80')